package model;

import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import utilities.HibernateUtil;
import entity.Host;

public class HostDAO {
    /**
     * Load Host in Shop.
     * @return List Host
     * @throws Exception
     */
    @SuppressWarnings({"unchecked", "finally"})
    public ArrayList<Host> getList() throws Exception {
        ArrayList<Host> list = new ArrayList<Host>();
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            list = (ArrayList<Host>) session.createCriteria(Host.class).list();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {

        }
        session.close();
        return list;
    }

    /**
     * Add New Host
     * @param Host
     * @throws Exception
     */
    public static void add(Host host) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(host);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Update Host
     * @param Host
     * @throws Exception
     */
    public static void update(Host host) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.update(host);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Delete Host
     * @param Host
     * @throws Exception
     */
    public static void delete(Host host) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.delete(host);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Find Host by ID
     * @param id
     * @return Host
     * @throws Exception
     */
    @SuppressWarnings("finally")
    public static Host find(int id) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        Host host = new Host();
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            host = (Host) session.get(Host.class, id);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
            return host;
        }
    }
}
